package com.sys.basics.utils;

import com.sys.basics.config.shiro.MyShiroRealm;
import com.sys.basics.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;

public class ShiroUtils {

    public static Subject getSubject(){
        return SecurityUtils.getSubject();
    }

    public static Session getSession(){
        return getSubject().getSession();
    }

    public static SysUser getUserInfo(){
        return (SysUser) getSubject().getPrincipal();
    }

    public static Long getUserId(){
        return getUserInfo().getId();
    }

    public static void updatePrincipal(SysUser userInfo){
        PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(userInfo, MyShiroRealm.class.getName());
        SecurityUtils.getSubject().runAs(newPrincipalCollection);
    }
}
